#!/Users/qihao/python-envs/clicker/bin/python3
from pynput import mouse, keyboard
import time
import threading

# 全局变量控制点击状态
clicking = False
click_thread = None

def clicking_task():
    m = mouse.Controller()
    while clicking:
        m.click(mouse.Button.left)
        time.sleep(0.1)  # 500ms延迟

def on_press(key):
    global clicking, click_thread
    
    if key == keyboard.Key.alt_l and hasattr(on_press, 'last_alt_press'):
        # 检查是否是快速双击alt
        if time.time() - on_press.last_alt_press < 0.3:  # 300ms内双击
            if not clicking:  # 如果当前没有在点击，就开始点击
                clicking = True
                click_thread = threading.Thread(target=clicking_task)
                click_thread.start()
            else:  # 如果正在点击，就停止点击
                clicking = False
                if click_thread:
                    click_thread.join()
    
    if key == keyboard.Key.alt_l:
        on_press.last_alt_press = time.time()

# 初始化last_alt_press
on_press.last_alt_press = 0

# 监听键盘
with keyboard.Listener(on_press=on_press) as listener:
    listener.join() 